JS Beginner Wiki
Welcome to the JavaScript Beginner Wiki Concepts of JavaScript Hoisting (Variables and Functions) JavaScript while creating execution context (for execution) scans the entire code and sets aside memory space for all the variables and functions before executing the entire code. This phenomenon is called as Hoisting. As if all the variables and function declarations are pulled on top (hoisting). So, It doesn't matter where variable is declared (before or after it's use), It can be referred in code as JS Hoisting makes it accessible. What value it may hold depends on position of the code. JS automatically assign 'undefined' to variables. If variable is accessed before defining, it is still allowed but value is 'undefined'. (Unlike other languages like Java, Cpp - where this is compile time error) Undeclared variable access results in run-time error 'Uncaught ReferenceError: variable is not defined'. Hoisting of functions function declarations are also hoisted. However, functions that are assigned to variables are not hoisted. For example, the following code will work as expected due to function declaration hoisting. foo(); function foo() { alert("Hello!"); } However, the following example will fail spectacularly. The variable declaration for foois hoisted before the function call. However, since the assignment to foo is not hoisted, an exception is thrown for trying to call a non-function variable. foo(); var foo = function() { alert("Hello!"); }; Function Declaration Overrides Variable Declaration When Hoisted Both function declaration and variable declarations are hoisted to the top of the containing scope. And function declaration takes precedence over variable declarations (but not over variable assignment). As is noted above, variable assignment is not hoisted, and neither is function assignment. As a reminder, this is a function assignment: var myFunction = function () {}. Here is a basic example to demonstrate: // Both the variable and the function are named myName var myName; function myName () { console.log ("Rich"); } // The function declaration overrides the variable name console.log(typeof myName); // function Scope chain (function outer reference) Outer reference of a function is context (another function or global execution context) where that function physically sits. when-ever a variable is referenced in JS, it looks first within the function, if not found it moves down the scope chain to it's outer reference and so on (till GEC). JS ObjectLiteral vs JSON JSON is popular info. exchange format derived from JS Objects. But JSON is more stricter - properties must be surrounded by quotes, functions are not allowed. All JSON are valid JS objects. But not vice versa. JS provides inbuilt functions to convert Objects into JSON - JSON.strigify(object) from JSON to object JSON.parse(json) Functional Programming JS functions are objects. They are also termed as first class objects. Functions can be passed around to another functions as parameters. And can be invoked separately. Main reason for JS termed as 'Functional Programming' Some other Function properties in JS Function can be called with few arguments also, It automatically calls and sets value undefined to not passed arguments. Function overloading is not available in JS, There are other alternative patterns which can achieve same here. Conclusion Latest activity Photos and videos are a great way to add visuals to your wiki. Find videos about your topic by exploring Fandom's Video Library. Category:Browse